﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WeChat.Model;
using WeChat.ViewModel;

namespace WeChat.View
{
    /// <summary>
    /// ContactView.xaml 的交互逻辑
    /// </summary>
    public partial class ContactView : UserControl
    {
        public ContactView()
        {
            InitializeComponent();
            this.DataContext = new ContactViewModel();
            string Initial = "";
            List<ContactModel> ContactList = new List<ContactModel>();
            for (int i = 1; i < 36; i++)
            {
                if (i >= 1 && i < 6)
                {
                    Initial = "A";
                }
                else if (i >= 6 && i < 12)
                {
                    Initial = "B";
                }
                else if (i >= 12 && i < 18)
                {
                    Initial = "C";
                }
                else if (i >= 18 && i < 24)
                {
                    Initial = "D";
                }
                else if (i >= 24)
                {
                    Initial = "E";
                }
                ContactList.Add(new ContactModel { Id = i, Name = $"昵称{i}", Avatar = "../Assets/temp/Images/avatar.jpg", Initial = Initial });
            }
            this.ContactListView.ItemsSource = ContactList;
            CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(ContactListView.ItemsSource);
            PropertyGroupDescription groupDescription = new PropertyGroupDescription("Initial");
            view.GroupDescriptions.Add(groupDescription);
        }

        private void OnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            TextBox SearchTextBox = sender as TextBox;
            SearchTextBox.Focus();
            SearchTextBox.Text = "";
            CloseSearchButton.Visibility = Visibility.Visible;
        }

        private void CloseSearchButton_Click(object sender, RoutedEventArgs e)
        {
            SearchTextBox.Text = "搜索";
            //TextBox失去焦点
            TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
            SearchTextBox.MoveFocus(request);
            CloseSearchButton.Visibility = Visibility.Hidden;
        }

        // 如何操作呢？

        //[
        // {
        //  "index": "a"
        //     childred:[
        //  {
        //   "name":"姓名",
        //   "avtu":"头像"

        //        }
        //  ]
        // }
        //]
    }
}
